require('dotenv').config()
const { consola } = require('consola')
const mongoose = require('mongoose')

const { MONGODB_URI: uri } = require('../utils/config')
consola.start('connected datebase...')
mongoose
  .connect(uri)
  .then(() => {
    consola.success('connected to MongoDB')
  })
  .catch((error) => {
    consola.error(new Error('error connecting to MongoDB:' + error.message))
  })

const noteSchema = new mongoose.Schema({
  content: {
    type: String,
    required: true,
  },
  date: {
    type: Date,
    required: true,
  },
  user: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
  },
})

noteSchema.set('toJSON', {
  transform: (document, returnedObject) => {
    returnedObject.id = returnedObject._id.toString()
    delete returnedObject._id
    delete returnedObject.__v
  },
})

module.exports = mongoose.model('Notes', noteSchema)
